package com.ruoyi.web.controller.gas;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gas.domain.GasDevice;
import com.ruoyi.gas.service.IGasDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import springfox.documentation.spring.web.json.Json;

/**
 * 设备信息Controller
 *
 * @author ruoyi
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/gas/device")
public class GasDeviceController extends BaseController
{
    @Autowired
    private IGasDeviceService gasDeviceService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('gas:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(GasDevice gasDevice)
    {
        startPage();
        List<GasDevice> list = gasDeviceService.selectGasDeviceList(gasDevice);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('gas:device:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GasDevice gasDevice)
    {
        List<GasDevice> list = gasDeviceService.selectGasDeviceList(gasDevice);
        ExcelUtil<GasDevice> util = new ExcelUtil<GasDevice>(GasDevice.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('gas:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") String deviceId)
    {
        return success(gasDeviceService.selectGasDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('gas:device:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GasDevice gasDevice)
    {
        return toAjax(gasDeviceService.insertGasDevice(gasDevice));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('gas:device:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GasDevice gasDevice)
    {
        return toAjax(gasDeviceService.updateGasDevice(gasDevice));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('gas:device:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable String[] deviceIds)
    {
        return toAjax(gasDeviceService.deleteGasDeviceByDeviceIds(deviceIds));
    }

    /**
     * 接收设备变更事件数据
     * 发生设备的新增或者设备信息的修改时，会推送该事件数据
     * 接收类型 1001
     * ZYJ
     * @param gasDevice
     * @return
     */
    @PostMapping("/changeDeviceMessage")
    public AjaxResult changeDeviceMessage(@RequestBody GasDevice gasDevice){
        System.out.println("设备变更事件接收数据================"+gasDevice);
        GasDevice device = gasDeviceService.selectGasDeviceByDeviceId(gasDevice.getDeviceId());

        int flag;
        if (device==null){
            //新增设备
            gasDevice.setCreateTime(DateUtils.getNowDate());
            flag=gasDeviceService.insertGasDevice(gasDevice);
        }else {
            //修改设备
            gasDevice.setUpdateTime(DateUtils.getNowDate());
            flag=gasDeviceService.updateGasDevice(gasDevice);
        }

        if (flag>0) {
            return new AjaxResult(HttpStatus.SUCCESS, "成功接收数据");
        }else {
            return new AjaxResult(400, "数据接收失败");
        }
    }

    /**
     * 接收设备删除事件数据
     * 发生设备的删除时，会推送该事件数据
     * 接收类型 1002
     * ZYJ
     * @param deviceId
     * @return
     */
    @PostMapping("/removeDeviceMessage")
    public AjaxResult removeDeviceMessage(@RequestBody String deviceId){
        System.out.println("设备删除事件接收数据==============="+deviceId);
        int flag = gasDeviceService.deleteGasDeviceByDeviceId(JSON.parseObject(deviceId).getString("deviceId"));

        if (flag>0) {
            return new AjaxResult(HttpStatus.SUCCESS, "成功接收数据");
        }else {
            return new AjaxResult(400, "数据接收失败");
        }
    }

    /**
     * 查询设备集合（id和名称）
     * @return
     */
    @GetMapping("/selectDeviceList")
    public AjaxResult selectDeviceList(){
        return AjaxResult.success(gasDeviceService.selectDeviceList());
    }


}
